package me.imli.app.ui;

import java.io.ByteArrayInputStream;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import com.android.volley.VolleyError;

import me.imli.app.Const;
import me.imli.app.R;
import me.imli.app.dao.RssDataHelper;
import me.imli.app.model.RssFeedModel;
import me.imli.app.model.RssItemModel;
import me.imli.app.ui.adapter.RssItemAdapter;
import me.imli.app.volley.RequestListener;
import me.imli.app.xml.RSSHandler;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity {
	
	@ViewById(R.id.swl_main)
	SwipeRefreshLayout mRefreshLayout;
	
	@ViewById(R.id.lv_main)
	ListView mListView;
	@Bean
	RssItemAdapter mRssAdapter;
	
	private RssDataHelper mRssDB;

	@AfterViews
	void init() {

		mRssDB = new RssDataHelper(getActivity());
		
		mListView.setAdapter(mRssAdapter);
		mRefreshLayout.setOnRefreshListener(onRefreshListener());
		
		startRefresh();
	}
	
	void startRefresh() {
		mRefreshLayout.setRefreshing(true);
		get(Const.BLOG_RSS, requestListener());
	}
	
	private SwipeRefreshLayout.OnRefreshListener onRefreshListener() {
		return new SwipeRefreshLayout.OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				get(Const.BLOG_RSS, requestListener());
			}
		};
	}
	
	private RequestListener requestListener() {
		return new RequestListener() {
			
			@Override
			public void requestSuccess(String json) {
				// TODO Auto-generated method stub
				
				
				RssFeedModel feedModel = RSSHandler.parseRssXml(new ByteArrayInputStream(json.getBytes()));
				
				if (feedModel != null) {
					mRssDB.insert(feedModel);
					mRssAdapter.update(feedModel.items);
				} else {
					Log.i(TAG, "null");
					mRssAdapter.update(mRssDB.query().items);
				}
				
				stopRefresh();
			}
			
			@Override
			public void requestError(VolleyError e) {
				// TODO Auto-generated method stub
				stopRefresh();
			}
		};
	}
	
	@UiThread(delay = 2000)
	void stopRefresh() {
		mRefreshLayout.setRefreshing(false);
	}
	
	@ItemClick(R.id.lv_main)
	void onItem(RssItemModel model) {
		Intent intent = new Intent(getActivity(), DetailActivity_.class);
		intent.putExtra(DetailActivity.INTENT_DATA, model);
		startActivity(intent);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			Intent intent = new Intent(getActivity(), SettingActivity_.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
